import axios from 'axios';

const APIHOST = process.env.APIHOST;

/**
 * 获取接口数据
 * @param {*} 接口地址
 * @param {*} 参数
 * @param {*} Http Method
 */
const fetch = (url, params, method) => {
  let config = {
    url: `${APIHOST}${url}`,
    method: method || 'get',
  };
  if (method === 'get') {
    config.params = params;
  } else {
    config.data = params;
  }
  return new Promise((resovle, reject) => {
    axios(config)
      .then(res => {
        if (res.data && res.data.result) {
          resovle(res.data.body);
        } else {
          console.error(res);
          reject(res.data.message || '未知错误');
        }
      })
      .catch(ex => {
        console.error(ex);
        reject(ex);
      });
  });
};

export default fetch;

